package com.suray.rgv.son.data;

import com.suray.basic.wcs.utils.typedef.Byte;
import com.suray.basic.wcs.utils.typedef.Word;

public class ModbusReadRecMessage extends ModbusBaseMessage {
    /**
     * @param description :报文的说明信息
     * @param isBig
     */
    public ModbusReadRecMessage(int dataSize) {
        super("子车modbus读返回", true);

        baseDatas.add(new Byte("slaveAddr", "从机地址"));
        baseDatas.add(new Byte("funcCode", "功能代码"));
        baseDatas.add(new Byte("dataSize", "数据长度"));

        for (int i = 0; i < dataSize; i++) {
            baseDatas.add(new Word("data" + i, "数据"));
        }

        baseDatas.add(new Word("crc", "crc校验"));
    }



    public int getSlaveAddr() {
        return getValue("slaveAddr");
    }

    public int getCrc() {
        return getValue("crc");
    }

    public void validateMessage(int slaveAddr, int startAddr, int crc) {
        if (getSlaveAddr() != slaveAddr) {
            throw new RuntimeException("从机地址错误:" + getSlaveAddr());
        }
        if (getCrc() != crc) {
            throw new RuntimeException("crc错误:" + getCrc() + "--" + crc);
        }
    }

    public int getDataSize() {
        return getValue("dataSize");
    }

    public int getData(int index) {
        return getValue("data" + index);
    }
}
